Column

Number of every kind of product ordered in milk aisle (at least 500 orderd)

Column

Days since prior order of each aisle in snacks department

Number of each kind of products ordered in fresh fruits aisle over a day

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
---

```{r setup, include=FALSE}

library(flexdashboard)

library(tidyverse)
library(plotly)

library(p8105.datasets)

data("instacart")
instacart = 
  instacart %>% 
  as_tibble(instacart)
```

Column {data-width=400}
-----------------------------------------------------------------------

### Number of every kind of product ordered in milk aisle (at least 500 orderd)

```{r}

instacart %>% 
  filter(aisle == "milk") %>%
  group_by(aisle, product_name) %>%
  summarize(n = n()) %>% 
  filter(n >= 500) %>%
  mutate(product_name = fct_reorder(product_name, n)) %>% 
  mutate(text_label = str_c("Aisle: ", aisle)) %>% 
  plot_ly(x = ~product_name, y = ~n, color = ~product_name, text = ~text_label, type = "bar", colors = "viridis")

```

Column {data-width=300}
-----------------------------------------------------------------------

### Days since prior order of each aisle in snacks department

```{r }

instacart %>% 
  filter(reordered == 1) %>%
  filter(department == "snacks") %>%
  mutate(aisle = fct_reorder(aisle, days_since_prior_order)) %>%
  plot_ly(y = ~days_since_prior_order, color = ~aisle, type = "box", colors = "viridis")

```

### Number of each kind of products ordered in fresh fruits aisle over a day

```{r }
instacart %>% 
  filter(aisle == "fresh fruits") %>%
  group_by(order_hour_of_day, product_name) %>%
  summarize(n = n()) %>%
  mutate(text_label = str_c("Product name: ", product_name)) %>% 
  plot_ly(
    x = ~order_hour_of_day, y = ~n, type = "scatter", mode = "markers",
    color = ~product_name, text = ~text_label, alpha = 0.6)
```